Ext.ns("Ext.baiyue");

var webPath = "/bookshop-admin";

Ext.baiyue.UserMngMain = function(config){
	
	var buttonWidth = 60;
	var gridWidth   = 150;
	var operationHtml = '<div><span onclick="deleteAdmin()">删除</span> | <span onclick="modifyAdmin()">修改</span></div>';
	//工具栏
	var adminBar = new Ext.Toolbar({
		items  : [{
			text  : '新增',
			style : 'align:center',
			width : buttonWidth,
			icon  : webPath+'/images/loginbackground.jpg',
			handler: function(){
				addUser();
			},
		},'|',{
			text  : '修改',
			style : 'align:center',
			width : buttonWidth,
			icon  : webPath+'/images/loginbackground.jpg',
			handler: function(){
				modifyUser();
			}
		},'|',{
			text  : '删除',
			style : 'align:center',
			width : buttonWidth,
			icon  : webPath+'/images/loginbackground.jpg',
			handler: function(){
				deleteUser();
			}
		}]
	});
	//model
	Ext.define("adminInfo",{
		extend 	: 'Ext.data.Model',
		fields 	: [{
			name 	: "adminId",
			type 	: 'string',
		},{
			name : 'email',
			type : 'string',
		},{
			name : 'name',
			type : 'string',
		},{
			name : 'role',
			type : 'string',
		}]
	});
	//controller
	var userMngStore = new Ext.data.Store({
		model   : 'adminInfo',
		pageSize: 10,
		proxy   : {
			type : 'ajax',
			url  : "list.do",
			reader: {
				type : 'json',
				root : "root",
			},
			autoLoad:true,
		}
	});
	//view
	var userMngInfoGrid = new Ext.grid.Panel({
		tbar   	:  userMngBar,
		store  	:  userMngStore,
		header	: false,
        border	: false,
        draggable: false,
        frame	: false,
        resizable: false,
		region : 'center',
		layout : 'fit',
		width  : '100%',
		columns: [{
			header : 'ID',
			width  : gridWidth,
			dataIndex: 'adminId',
		},{
			header : "名称",
			width  : gridWidth,
			dataIndex: 'name',
		},{
			header : "邮件",
			width  : gridWidth,
			dataIndex: 'email',
		},{
			header : "角色",
			width  : gridWidth,
			dataIndex: 'role',
		},{
			id     : "operation",
			flex   : 1,           //自动占满剩余空间.
			header : "操作",
			renderer: function(){
				return operationHtml;
			}
		}],
		columnLines : true,
		emptyMsg : "没有管理员",
	});

	Ext.create('Ext.Viewport', {
			layout : 'border',
			items : [ adminInfoGrid ]
	});
	
	
	var deleteAdmin = function(){
		alert("delete");
	}
	var addAdmin = function(){
		alert("add");
	}
	var modifyAdmin = function(){
		alert("modify");
	}
	adminStore.load();
}